package com.yao.component.circle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.yao.sdk.util.Utils;

import java.util.ArrayList;

public class PictureBrowseActivity extends Activity {

    private String[] imageArray;
    private ArrayList<String> imageList;
    private int position;
    public static final String IMAGE_URL_ARRAY = "imageArray";
    public static final String IMAGE_URL_LIST = "imageList";
    public static final String IMAGE_URL_POSITION = "position";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_view);
        Intent intent = getIntent();
        imageList = intent.getStringArrayListExtra(IMAGE_URL_LIST);
        if (Utils.isEmpty(imageList)) {
            imageArray = intent.getStringArrayExtra(IMAGE_URL_ARRAY);
        } else {
            imageArray = new String[imageList.size()];
            for (int i = 0; i < imageList.size(); i++) {
                imageArray[i] = imageList.get(i);
            }
        }
        if (imageArray == null || imageArray.length < 1) {
            Toast.makeText(this, "图片路径解析错误", Toast.LENGTH_SHORT).show();
        }
        position = intent.getIntExtra(IMAGE_URL_POSITION,0);
        ExtendedViewPager mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);
        mViewPager.setAdapter(new TouchImageAdapter());
        mViewPager.setCurrentItem(position);
    }

    class TouchImageAdapter extends PagerAdapter {


        @Override
        public int getCount() {
            return imageArray.length;
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            TouchImageView img = new TouchImageView(container.getContext());
            Picasso.with(PictureBrowseActivity.this).load(imageArray[position]).placeholder(R.drawable.default_avatar_img).into(img);
            container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PictureBrowseActivity.this.finish();
                }
            });
            return img;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

}
